var sysId = "";
var nodeId = "";
var nodeName = "";
var tableName = "";
var nodeTypeNew = "";
var editIndex = undefined;
var comboData;
/**
 * 用于单元格编辑是重写的方法
 */
$.extend($.fn.datagrid.methods, {
    editCell: function(jq, param) {
        return jq.each(function() {
            var fields = $(this).datagrid('getColumnFields', true).concat(
                $(this).datagrid('getColumnFields'));
            for (var i = 0; i < fields.length; i++) {
                var col = $(this).datagrid('getColumnOption', fields[i]);
                col.editor1 = col.editor;
                if (fields[i] != param.field) {
                    var fieldOpts = $(this).datagrid('getColumnOption',
                        fields[i]);
                    if (!fieldOpts.hidden || fieldOpts.hidden == false) {
                        col.editor = null;
                    }
                }
            }
            $(this).datagrid('beginEdit', param.index);
            for (var i = 0; i < fields.length; i++) {
                var col = $(this).datagrid('getColumnOption', fields[i]);
                col.editor = col.editor1;
            }
        });
    },
    addEditor: function(jq, param) {
        if (param instanceof Array) {
            $.each(param, function(index, item) {
                var e = $(jq).datagrid('getColumnOption', item.field);
                e.editor = item.editor;
            });
        } else {
            var e = $(jq).datagrid('getColumnOption', param.field);
            e.editor = param.editor;
        }
    },
    removeEditor: function(jq, param) {
        if (param instanceof Array) {
            $.each(param, function(index, item) {
                var e = $(jq).datagrid('getColumnOption', item);
                e.editor = {};
            });
        } else {
            var e = $(jq).datagrid('getColumnOption', param);
            e.editor = {};
        }
    }
});

/**
 * 编辑数据
 */
function endEditing() {
    if (editIndex == undefined) {
        return true;
    }
    if ($('#cbbIndexTabColumnsDataGrid').datagrid('validateRow', editIndex)) {
        $('#cbbIndexTabColumnsDataGrid').datagrid('endEdit', editIndex);
        return true;
    } else {
    	parent.parent.parent.$.messager.show({
            title:commonLang.tip.alert,// '提示',
            //commonLang.tip.checkData"请检查数据是否正确！"
            msg: commonLang.tip.checkData,
            timeout: 3000,
            showType: 'slide'
        });
        return false;
    }
}

/**
 * 追加行
 */
function append() {
    if (endEditing()) {
        // 删除编辑器
        $("#cbbIndexTabColumnsDataGrid").datagrid('removeEditor', 'COL_LABEL');
        // 添加编辑器
        $("#cbbIndexTabColumnsDataGrid")
            .datagrid(
                'addEditor', {
                    field: 'COL_LABEL',
                    editor: {
                        type: 'combobox',
                        options: {
                            valueField: 'id',
                            textField: 'text',
                            data: comboData,
                            editable: false,
                            multiple: true,
                            formatter: function(row) {
                                var opts = $(this).combobox('options');
                                return row[opts.textField];
                            },
                            onSelect: function(record) { // 选中填值
                                // 获取dataGrid中未保存数据的indexName
                                var insertRows = $(
                                        "#cbbIndexTabColumnsDataGrid")
                                    .datagrid('getChanges',
                                        'inserted');
                                var insertData = "";
                                if (insertRows == "") {
                                    insertData = "";
                                } else {
                                    insertData = JSON
                                        .stringify(insertRows);
                                }
                                $
                                    .ajax({
                                        url: 'platform/aos/commtablemanagerController/getColIndexNameData.json',
                                        data: {
                                            insertData: insertData,
                                            colId: record.id,
                                            sysId: sysId,
                                            nodeId: nodeId,
                                            nodeName: nodeName,
                                            tableName: tableName
                                        },
                                        type: 'post',
                                        dataType: 'json',
                                        success: function(result) {
                                            // 给该单元格充值
                                            var indexName = result.indexName;
                                            var colName = result.colName;
                                            var colId = result.colId;
                                            var editors = $(
                                                    '#cbbIndexTabColumnsDataGrid')
                                                .datagrid(
                                                    'getEditors',
                                                    editIndex);
                                            // id
                                            var sfgzEditor1 = editors[1];
                                            var colIdShow = sfgzEditor1.target
                                                .val();
                                            if (colIdShow == '') {
                                                colIdShow = colId;
                                            } else {
                                                colIdShow = colIdShow + ',' + colId;
                                            }
                                            sfgzEditor1.target
                                                .val(colIdShow);
                                            // 字段名
                                            var sfgzEditor3 = editors[3];
                                            var colNameShow = sfgzEditor3.target
                                                .val();
                                            if (colNameShow == '') {
                                                colNameShow = colName;
                                                // 索引名
                                                var sfgzEditor0 = editors[0];
                                                sfgzEditor0.target
                                                    .val(indexName); // 设置索引名
                                            } else {
                                                colNameShow = colNameShow + ',' + colName;
                                            }
                                            sfgzEditor3.target
                                                .val(colNameShow);

                                        }
                                    });
                            },
                            onUnselect: function(record) { // 取消选中填值
                                $
                                    .ajax({
                                        url: 'platform/aos/commtablemanagerController/getColIndexNameData.json',
                                        data: {
                                            colId: record.id,
                                            sysId: sysId,
                                            nodeId: nodeId,
                                            nodeName: nodeName,
                                            tableName: tableName
                                        },
                                        type: 'post',
                                        dataType: 'json',
                                        success: function(result) {
                                            // 给该单元格充值
                                            var indexName = result.indexName;
                                            var colName = result.colName;
                                            var colId = result.colId;
                                            var editors = $(
                                                    '#cbbIndexTabColumnsDataGrid')
                                                .datagrid(
                                                    'getEditors',
                                                    editIndex);
                                            // 字段名
                                            var sfgzEditor3 = editors[3];
                                            var colNameShow = sfgzEditor3.target
                                                .val();
                                            // 去掉取消勾选的字段名
                                            var colNameStr = colNameShow
                                                .split(",");
                                            colNameShow = ""; // 置空
                                            for (var i = 0; i < colNameStr.length; i++) {
                                                if (colNameStr[i] != colName) {
                                                    colNameShow = colNameShow + "," + colNameStr[i];
                                                }
                                            }
                                            colNameShow = colNameShow
                                                .substring(
                                                    1,
                                                    colNameShow.length);
                                            sfgzEditor3.target
                                                .val(colNameShow);
                                            // 字段Id
                                            var sfgzEditor1 = editors[1];
                                            var colIdShow = sfgzEditor1.target
                                                .val();
                                            var colIdStr = colIdShow
                                                .split(",");
                                            colIdShow = "";
                                            for (var j = 0; j < colIdStr.length; j++) {
                                                if (colIdStr[j] != colId) {
                                                    colIdShow = colIdShow + "," + colIdStr[j];
                                                }
                                            }
                                            colIdShow = colIdShow
                                                .substring(
                                                    1,
                                                    colIdShow.length);
                                            sfgzEditor1.target
                                                .val(colIdShow);
                                            // 索引名
                                            var sfgzEditor0 = editors[0];
                                            if (colNameShow == "") {
                                                sfgzEditor0.target
                                                    .val("");
                                            }
                                        }
                                    });
                            }
                        }
                    }
                });
        $('#cbbIndexTabColumnsDataGrid').datagrid('appendRow', {
            ID: ''
        });
        editIndex = $('#cbbIndexTabColumnsDataGrid').datagrid('getRows').length - 1;
        $('#cbbIndexTabColumnsDataGrid').datagrid('selectRow', editIndex)
            .datagrid('beginEdit', editIndex);
        // 设置索引名称只读
        var cellEdit1 = $('#cbbIndexTabColumnsDataGrid').datagrid('getEditor', {
            index: editIndex,
            field: 'INDEX_NAME'
        });
        var $input1 = cellEdit1.target; // 得到文本框对象
        $input1.prop('readonly', true); // 设值只读
        // 设置字段名只读
        var cellEdit2 = $('#cbbIndexTabColumnsDataGrid').datagrid('getEditor', {
            index: editIndex,
            field: 'COL_NAME'
        });
        var $input2 = cellEdit2.target; // 得到文本框对象
        $input2.prop('readonly', true); // 设值只读
        $('#cbbIndexTabColumnsDataGrid').datagrid('endEdit', editIndex);
    }
}

/**
 * 删除所选的行
 */
function removeit() {
    var rows = $('#cbbIndexTabColumnsDataGrid').datagrid('getChecked');
    if (rows == "") {
        //commonLang.tip.alert'提示',commonLang.tip.content2'请选择要删除的记录
    	$.messager.alert(commonLang.tip.alert,commonLang.tip.content2,'warning');
        return;
    }
    $.messager
        .confirm(
                //commonLang.tip.confirm '请确认',commonLang.tip.confirmContent '您确定要删除当前所选的数据？'
        		commonLang.tip.confirm,
        		commonLang.tip.confirmContent,
            function(b) {
                if (b) {
                    // 删除字段
                    var deleteData = JSON.stringify(rows);
                    $
                        .ajax({
                            url: 'platform/aos/commtablemanagerController/deleteCbbTabIndexData.json',
                            data: {
                                deleteData: deleteData,
                                sysId: sysId,
                                nodeId: nodeId,
                                nodeName: nodeName,
                                tableName: tableName
                            },
                            type: 'post',
                            dataType: 'json',
                            success: function(result) {
                                if (result.flag == "failure") {
                                	parent.parent.parent.$.messager.show({
                                        title: commonLang.tip.alert ,//'提示',
                                        msg: result.mes
                                    });
                                } else {
                                	parent.parent.parent.$.messager.show({
                                        title: commonLang.tip.alert ,//'提示',
                                        //commonLang.optip.success"删除成功！"
                                        msg: commonLang.optip.success
                                    });
                                    // 表格中删除选中的行
                                    var num = JSON
                                        .stringify(rows.length);
                                    for (var j = 0; j < num; j++) {
                                        var row = rows[j];
                                        var index = $(
                                                '#cbbIndexTabColumnsDataGrid')
                                            .datagrid(
                                                'getRowIndex',
                                                row);
                                        $(
                                                '#cbbIndexTabColumnsDataGrid')
                                            .datagrid(
                                                'deleteRow',
                                                index);
                                    }
                                }
                            }
                        });
                }
            });
}
/**
 * 刷新表格数据
 */
function reloadDataGrid() {
    var url = 'platform/aos/commtablemanagerController/getCbbTabIndexData.json?sysId=' + sysId + '&tableName=' + tableName;
    $('#cbbIndexTabColumnsDataGrid').datagrid("options").url = url;
    $("#cbbIndexTabColumnsDataGrid").datagrid('load', {
        param: null
    });
}

/**
 * 提交改变的记录
 */
function accept() {
    if (endEditing()) {
        // 新增字段
        var insertRows = $("#cbbIndexTabColumnsDataGrid").datagrid(
            'getChanges', 'inserted');
        // 更新字段
        var updateRows = $("#cbbIndexTabColumnsDataGrid").datagrid(
            'getChanges', 'updated');
        if (insertRows == "" && updateRows == "") {
            //commonLang.tip.alert'提示',commonLang.tip.alert2'没有可保存的记录！'
        	$.messager.alert(commonLang.tip.alert,commonLang.tip.alert2,'warning');
            return false;
        }
        var insertData = JSON.stringify(insertRows);
        var updateData = JSON.stringify(updateRows);

        $
            .ajax({
                url: 'platform/aos/commtablemanagerController/saveCbbTabIndexData.json',
                data: {
                    insertData: insertData,
                    updateData: updateData,
                    sysId: sysId,
                    nodeId: nodeId,
                    nodeName: nodeName,
                    tableName: tableName
                },
                type: 'post',
                dataType: 'json',
                success: function(result) {
                    if (result.flag == "failure") {
                    	parent.parent.parent.$.messager.show({
                            title: commonLang.tip.alert,//'提示',
                            msg: result.mes
                        });
                    } else {
                        isOk = true;
                        parent.parent.parent.$.messager.show({
                            title: commonLang.tip.alert,//'提示',
                            //commonLang.optip.saveSuccess "保存成功！"
                            msg: commonLang.optip.saveSuccess
                        });
                    }
                    reloadDataGrid();
                }
            });
    }
}

/**
 * 字段名称转换
 * 
 * @param value
 * @param row
 * @param index
 * @returns
 */
function fomaterColNameType(value, row, index) {
    var showValue = "";
    if (row.ID == "") { // 说明是新增行
        if (value != undefined) {
            $('#cbbIndexTabColumnsDataGrid').datagrid('endEdit', index);
            $('#cbbIndexTabColumnsDataGrid').datagrid('selectRow', index)
                .datagrid('beginEdit', index);
            var ed = $('#cbbIndexTabColumnsDataGrid').datagrid('getEditor', {
                index: index,
                field: 'COL_LABEL'
            });
            var colLabel = $(ed.target).combobox('getText');
            showValue = colLabel;
        }
    } else { // 数据库加载行,把ID串转换为NAME
        //		$('#cbbIndexTabColumnsDataGrid').datagrid('endEdit', index);
        //		$('#cbbIndexTabColumnsDataGrid').datagrid('selectRow', index).datagrid(
        //				'beginEdit', index);
        //		var ed = $('#cbbIndexTabColumnsDataGrid').datagrid('getEditor', {
        //			index : index,
        //			field : 'COL_LABEL'
        //		});
        //		if (ed != null) {
        //			var colLabel = $(ed.target).combobox('getText');
        //			showValue = colLabel;
        //		} else {
        //			showValue = value;
        //		}
        var ids = value.split(',');
        if (ids.length > 0 && ids[0].length == 32) {
            for (var i = 0; i < ids.length; i++) {
                for (var j = 0; j < comboData.length; j++) {
                    if (ids[i] == comboData[j].id) {
                        showValue += comboData[j].text + ",";
                    }
                }
            }
            showValue = showValue.substring(0, showValue.length - 1);
        } else {
            showValue = value;
        }
    }
    var abValue = showValue;
    if (null != showValue && "" != showValue && 'undefined' != showValue) {
        if (showValue.length >= 25) {
            abValue = showValue.substring(0, 24) + "...";
        }
    } else {
        abValue = "";
    }
    var content = '<span href="#" title="' + showValue + '" class="note">' + abValue + '</span>';
    return content;
}

/**
 * 转换索引类型
 * 
 * @param value
 * @param row
 * @param index
 * @returns
 */
function fomaterIndexType(value, row, index) {
    var type = "";
    if (value == '1') {
        type = commonLang.fomaterType.commonIndex;//"普通索引";
    } else if (value == '2') {
        type = commonLang.fomaterType.uniqueIndex;//"唯一索引";
    }
    return type;
}
/*******************************************************************************
 * 编辑数据
 */
function onClickRow(rowIndex, rowData) {
    if (endEditing()) {
        // 删除编辑器
        $("#cbbIndexTabColumnsDataGrid").datagrid('removeEditor', 'COL_LABEL');
        // 添加编辑器
        $("#cbbIndexTabColumnsDataGrid")
            .datagrid(
                'addEditor', {
                    field: 'COL_LABEL',
                    editor: {
                        type: 'combobox',
                        options: {
                            valueField: 'id',
                            textField: 'text',
                            data: comboData,
                            editable: false,
                            multiple: true,
                            formatter: function(row) {
                                var opts = $(this).combobox('options');
                                return row[opts.textField];
                            },
                            onSelect: function(record) { // 选中填值
                                // 获取dataGrid中未保存数据的indexName
                                var insertRows = $(
                                        "#cbbIndexTabColumnsDataGrid")
                                    .datagrid('getChanges',
                                        'inserted');
                                var insertData = "";
                                if (insertRows == "") {
                                    insertData = "";
                                } else {
                                    insertData = JSON
                                        .stringify(insertRows);
                                }
                                $
                                    .ajax({
                                        url: 'platform/aos/commtablemanagerController/getColIndexNameData.json',
                                        data: {
                                            insertData: insertData,
                                            colId: record.id,
                                            sysId: sysId,
                                            nodeId: nodeId,
                                            nodeName: nodeName,
                                            tableName: tableName
                                        },
                                        type: 'post',
                                        dataType: 'json',
                                        success: function(result) {
                                            // 给该单元格充值
                                            var indexName = result.indexName;
                                            var colName = result.colName;
                                            var colId = result.colId;
                                            var editors = $(
                                                    '#cbbIndexTabColumnsDataGrid')
                                                .datagrid(
                                                    'getEditors',
                                                    editIndex);
                                            // id
                                            var sfgzEditor1 = editors[1];
                                            var colIdShow = sfgzEditor1.target
                                                .val();
                                            if (colIdShow == '') {
                                                colIdShow = colId;
                                            } else {
                                                colIdShow = colIdShow + ',' + colId;
                                            }
                                            sfgzEditor1.target
                                                .val(colIdShow);
                                            // 字段名
                                            var sfgzEditor3 = editors[3];
                                            var colNameShow = sfgzEditor3.target
                                                .val();
                                            if (colNameShow == '') {
                                                colNameShow = colName;
                                                // 索引名
                                                var sfgzEditor0 = editors[0];
                                                sfgzEditor0.target
                                                    .val(indexName); // 设置索引名
                                            } else {
                                                colNameShow = colNameShow + ',' + colName;
                                            }
                                            sfgzEditor3.target
                                                .val(colNameShow);
                                        }
                                    });
                            },
                            onUnselect: function(record) { // 取消选中填值
                                $
                                    .ajax({
                                        url: 'platform/aos/commtablemanagerController/getColIndexNameData.json',
                                        data: {
                                            colId: record.id,
                                            sysId: sysId,
                                            nodeId: nodeId,
                                            nodeName: nodeName,
                                            tableName: tableName
                                        },
                                        type: 'post',
                                        dataType: 'json',
                                        success: function(result) {
                                            // 给该单元格充值
                                            var indexName = result.indexName;
                                            var colName = result.colName;
                                            var colId = result.colId;
                                            var editors = $(
                                                    '#cbbIndexTabColumnsDataGrid')
                                                .datagrid(
                                                    'getEditors',
                                                    editIndex);
                                            // 字段名
                                            var sfgzEditor3 = editors[3];
                                            var colNameShow = sfgzEditor3.target
                                                .val();
                                            // 去掉取消勾选的字段名
                                            var colNameStr = colNameShow
                                                .split(",");
                                            colNameShow = ""; // 置空
                                            for (var i = 0; i < colNameStr.length; i++) {
                                                if (colNameStr[i] != colName) {
                                                    colNameShow = colNameShow + "," + colNameStr[i];
                                                }
                                            }
                                            colNameShow = colNameShow
                                                .substring(
                                                    1,
                                                    colNameShow.length);
                                            sfgzEditor3.target
                                                .val(colNameShow);
                                            // 字段Id
                                            var sfgzEditor1 = editors[1];
                                            var colIdShow = sfgzEditor1.target
                                                .val();
                                            var colIdStr = colIdShow
                                                .split(",");
                                            colIdShow = "";
                                            for (var j = 0; j < colIdStr.length; j++) {
                                                if (colIdStr[j] != colId) {
                                                    colIdShow = colIdShow + "," + colIdStr[j];
                                                }
                                            }
                                            colIdShow = colIdShow
                                                .substring(
                                                    1,
                                                    colIdShow.length);
                                            sfgzEditor1.target
                                                .val(colIdShow);
                                            // 索引名
                                            var sfgzEditor0 = editors[0];
                                            if (colNameShow == "") {
                                                sfgzEditor0.target
                                                    .val("");
                                            }
                                        }
                                    });
                            }
                        }
                    }
                });
        $('#cbbIndexTabColumnsDataGrid').datagrid('endEdit', rowIndex);
        $('#cbbIndexTabColumnsDataGrid').datagrid('selectRow', rowIndex)
            .datagrid('beginEdit', rowIndex);
        var editors = $('#cbbIndexTabColumnsDataGrid').datagrid('getEditors',
            rowIndex);
        if (editors != "") {
            var id = editors[1].target.val();
            // 给下拉列表设置初值
            editors[2].target.combobox('setValues', id.split(','));
            // 设置索引名称只读
            var cellEdit1 = $('#cbbIndexTabColumnsDataGrid').datagrid(
                'getEditor', {
                    index: rowIndex,
                    field: 'INDEX_NAME'
                });
            var $input1 = cellEdit1.target; // 得到文本框对象
            $input1.prop('readonly', true); // 设值只读
            // 设置字段名只读
            var cellEdit2 = $('#cbbIndexTabColumnsDataGrid').datagrid(
                'getEditor', {
                    index: rowIndex,
                    field: 'COL_NAME'
                });
            var $input2 = cellEdit2.target; // 得到文本框对象
            $input2.prop('readonly', true); // 设值只读
        }
        editIndex = rowIndex;
    } else {
        $('#cbbIndexTabColumnsDataGrid').datagrid('selectRow', editIndex);
    }
}